package com.monolithiot.cdn.management.controller;

import com.monolithiot.cdn.management.common.context.Constants;
import com.monolithiot.cdn.management.common.entity.Project;
import com.monolithiot.cdn.management.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 2018/11/24 Create by 郭文梁
 * ProjectPageController
 * 项目页面访问控制器
 *
 * @author 郭文梁
 * @date 2018/11/24
 */
@Controller
@RequestMapping("/project-page")
public class ProjectPageController extends BaseController {
    private final ProjectService projectService;

    @Autowired
    public ProjectPageController(ProjectService projectService) {
        this.projectService = projectService;
    }

    /**
     * 上传项目文件
     *
     * @param projectId 项目的ID
     * @param zipFile   项目文件
     * @return view name
     */
    @PostMapping("/_upload")
    public String upload(Integer projectId, MultipartFile zipFile, Map<String, Object> model) throws IOException {
        Project project = projectService.findById(projectId);
        if (project == null) {
            model.put("errMsg", "项目不存在");
            return Constants.View.ERROR;
        }

        Project res = projectService.setZipFile(project, zipFile);
        model.put("project", res);
        return Constants.View.SUCCESS;
    }
}
